﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Security.Cryptography;
using System.Text;
using SQLExecute;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;
using System.Text.RegularExpressions;

public partial class users_list_users : System.Web.UI.Page
{
    User u = new User();
    static int id;
    public string strMessage;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["username"] != null && Session["fullname"] != null)
           {

                Init_Combo();
                BindGrid();
                lUsername.Text = Session["username"].ToString();
            }
            else Response.Redirect("../login.aspx");
        }
    }
    protected void Init_Combo()
    {
        string sql = "select * from tbl_groupuser where id=2";
        DataSet ds = SqlHelper.ExecuteDataset(Common.strConnect, CommandType.Text, sql);
        cbGroupUser.DataSource = ds.Tables[0];
        cbGroupUser.DataTextField = "groupname";
        cbGroupUser.DataValueField = "id";
        cbGroupUser.DataBind();
      
        sql = "select * from tbl_phongban";
        ds = SqlHelper.ExecuteDataset(Common.strConnect, CommandType.Text, sql);
        cbPhongBan.DataSource = ds.Tables[0];
        cbPhongBan.DataTextField = "phongban";
        cbPhongBan.DataValueField = "id";
        cbPhongBan.DataBind();
        cbPhongBan.Items.Insert(0, new ListItem("Choose one", "Choose one"));
        cbPhongBan.SelectedIndex = 0;

    }
    protected void BindGrid()
    {
        User u = new User();
        DataSet ds = u.Get_List_Users();
        gdUsers.DataSource = ds;
        gdUsers.DataBind();
        gdUsers.Columns[1].Visible = false;
    
    }
    protected void ClearTextControls()
    {
        txtFullname.Text = "";
        txtUserName.Text = "";
        txtEmail.Text = "";
    }
    protected bool CheckValid()
    {
        if (txtFullname.Text != "")
        {
            strMessage = "Vui lòng nhập họ tên!.";

            return false;
        }

        if (u.ValidMail(txtEmail.Text))
        {

            strMessage = "Địa chỉ email không hợp lệ!.";
            return false;
        }


        return true;
    
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string s = string.Empty;
        string strPassword=string.Empty;

        if (CheckValid())
        {
            strPassword = u.RandomPassword();
            s = u.Encrypt_Pass();
            u.FullName = txtFullname.Text;
            u.UserName = txtUserName.Text;
            if (cbPhongBan.SelectedIndex > 0)
                u.PhongBanID = Convert.ToInt16(cbPhongBan.SelectedValue);

            u.GroupID = Convert.ToInt16(cbGroupUser.SelectedValue);
            u.Email = txtEmail.Text;

            if (chkActive.Checked)
                u.Insert_New_User(s, true);
            else
                u.Insert_New_User(s, false);
            u.SendMail("quangtran@vus-etsc.edu.vn", txtEmail.Text, "Welcome to " + txtFullname.Text, 1);
            ClearTextControls();
            BindGrid();

        }
        else
        
            popupMessage.Show();
        


       
    }
    private void Del_User(int nID)
    {
        string sql = "delete tbl_users where id='" + nID + "'";

        SqlHelper.ExecuteNonQuery(Common.strConnect, CommandType.Text, sql);



    }
    protected void gdUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Del_User(id);
    }
    protected void gdUsers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            id =Convert.ToInt16(e.CommandArgument.ToString());
            Del_User(id);
            BindGrid();
        }
    }
    protected void lnkLogout_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
       
        Session.Clear();
        Response.Redirect("../login.aspx");
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        
    }
    protected void gdUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdUsers.PageIndex = e.NewPageIndex;
        BindGrid();
    }
}
